package com.creditcmsplt.util;

import java.io.File;
import java.util.ArrayList;
import java.util.Arrays;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import net.lingala.zip4j.core.ZipFile;
import net.lingala.zip4j.exception.ZipException;
import net.lingala.zip4j.model.ZipParameters;
import net.lingala.zip4j.util.Zip4jConstants;

/**
 * zip压缩工具
 * @author 雷阳军
 *
 */
public class ZipUtil {

	private static Logger log = LoggerFactory.getLogger(ZipUtil.class);
	
	/**
     * 添加多个文件到压缩包中，并且设置密码。
     *
     * @param targetZip:压缩目标,及压缩包名称,如D://abc.zip
     * @param filesToAdd:要压缩的文件列表
     * @param pwd:压缩密码,为空则没有密码
     */
    public static void AddFilesWithPwd(String targetZip, ArrayList<File> filesToAdd, String pwd) {
        try {
            ZipFile zipFile = new ZipFile(targetZip);
            ZipParameters parameters = new ZipParameters();
            parameters.setCompressionMethod(Zip4jConstants.COMP_DEFLATE); // set compression method to deflate compression
            parameters.setCompressionLevel(Zip4jConstants.DEFLATE_LEVEL_NORMAL);
            if (pwd != null && pwd.length() > 0) {
                parameters.setPassword(pwd);
                parameters.setEncryptFiles(true);
                parameters.setEncryptionMethod(Zip4jConstants.ENC_METHOD_STANDARD);
                //parameters.setAesKeyStrength(Zip4jConstants.AES_STRENGTH_256);
            }
            zipFile.addFiles(filesToAdd, parameters);
        } catch (ZipException e) {
        	log.error("",e);
        }
    }
    
    /**
     * 添加多个文件到压缩包中，并且设置密码。
     *
     * @param targetZip:压缩目标,及压缩包名称,如D://abc.zip
     * @param filesToAdd:要压缩的文件列表
     * @param pwd:压缩密码,为空则没有密码
     */
    public static void AddFilePathsWithPwd(String targetZip, ArrayList<String> filesToAdd, String pwd) {
        try {
            ZipFile zipFile = new ZipFile(targetZip);
            ZipParameters parameters = new ZipParameters();
            parameters.setCompressionMethod(Zip4jConstants.COMP_DEFLATE); // set compression method to deflate compression
            parameters.setCompressionLevel(Zip4jConstants.DEFLATE_LEVEL_NORMAL);
            if (pwd != null && pwd.length() > 0) {
                parameters.setPassword(pwd);
                parameters.setEncryptFiles(true);
                parameters.setEncryptionMethod(Zip4jConstants.ENC_METHOD_STANDARD);//ENC_METHOD_AES
                //parameters.setAesKeyStrength(Zip4jConstants.AES_STRENGTH_256);
            }
            ArrayList<File> fileSources=new ArrayList<File>();
            for (String string : filesToAdd) {
            	File file=new File(string);
            	fileSources.add(file);
			}
            zipFile.addFiles(fileSources, parameters);
        } catch (ZipException e) {
        	log.error("",e);
        }
    }
    
    /**
     * 压缩单个文件到压缩包
     * @param targetZip:压缩目标,及压缩包名称,如D://abc.zip
     * @param file:要压缩的文件
     * @param pwd:压缩密码,为空则没有密码
     */
    public static void AddFileWithPwd(String targetZip, File file, String pwd) {
        try {
            ZipFile zipFile = new ZipFile(targetZip);
            ZipParameters parameters = new ZipParameters();
            parameters.setCompressionMethod(Zip4jConstants.COMP_DEFLATE); // set compression method to deflate compression
            parameters.setCompressionLevel(Zip4jConstants.DEFLATE_LEVEL_NORMAL);
            if (pwd != null && pwd.length() > 0) {
                parameters.setPassword(pwd);
                parameters.setEncryptFiles(true);
                parameters.setEncryptionMethod(Zip4jConstants.ENC_METHOD_STANDARD);
                //parameters.setAesKeyStrength(Zip4jConstants.AES_STRENGTH_256);
            }
            zipFile.addFile(file, parameters);
        } catch (ZipException e) {
        	log.error("",e);
        }
    }
    
    /**
     * 解压zip文件
     * @param targetZip:原文件zip路径
     * @param destPath:
     * @param pwd
     */
    public static void unzip(String sourceZip, String destPath, String pwd) {
        try {
            ZipFile zipFile = new ZipFile(sourceZip);
            if(!TextUtils.isEmpty(pwd)){
            	zipFile.setPassword(pwd);
            }
            zipFile.extractAll(destPath);
        } catch (ZipException e) {
        	log.error("",e);
        }
    }
    
    public static void main(String[] args) {
    	File abc=new File("G:/file");
    	ArrayList<File> arrayList = new ArrayList<File>(Arrays.asList(abc.listFiles()));
    	AddFilesWithPwd("G:/ab.zip", arrayList, "ab12cd34");
    	
    	unzip("G:/ab.zip", "G:/ab", "ab12cd34");
	}
}
